/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti.explorer.ui.task;

import com.vaadin.ui.ComboBox;
import org.activiti.explorer.Constants;
import org.activiti.explorer.I18nManager;
import org.activiti.explorer.Messages;

import java.util.Arrays;

/**
 * @author Joram Barrez
 */
public class PriorityComboBox extends ComboBox {

    private static final long serialVersionUID = 1L;

    protected I18nManager i18nManager;

    public PriorityComboBox(I18nManager i18nManager) {
        super(null, Arrays.asList(
                i18nManager.getMessage(Messages.TASK_PRIORITY_LOW),
                i18nManager.getMessage(Messages.TASK_PRIORITY_MEDIUM),
                i18nManager.getMessage(Messages.TASK_PRIORITY_HIGH)));
        this.i18nManager = i18nManager;
        setValue(i18nManager.getMessage(Messages.TASK_PRIORITY_LOW));
        setNullSelectionAllowed(false);
        setInvalidAllowed(false);
        setImmediate(true);
        setWidth(125, UNITS_PIXELS);
    }

    public PriorityComboBox(I18nManager i18nManager, Object value) {
        this(i18nManager);

        setValue(value);
    }

    public int getPriority() {
        String value = getValue().toString();
        if (i18nManager.getMessage(Messages.TASK_PRIORITY_LOW).equals(value)) {
            return Constants.TASK_PRIORITY_LOW;
        } else if (i18nManager.getMessage(Messages.TASK_PRIORITY_MEDIUM).equals(value)) {
            return Constants.TASK_PRIORITY_MEDIUM;
        } else {
            return Constants.TASK_PRIORITY_HIGH;
        }
    }

}
